package com.orgname.glvone.glvtwo.common.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.orgname.glvone.glvtwo.common.group.Insert;
import com.orgname.glvone.glvtwo.common.group.Update;
import com.orgname.glvone.glvtwo.common.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * @Author: guoliwei
 * @Date: 2021/10/31 19:34
 * @Version 1.0
 */
@Api
@Slf4j
@Validated
@RestController
public abstract class BaseController<S extends IService<T>, T> {

    protected abstract S getService();

    @Operation(summary = "list")
    @PostMapping("list")
    public Response list(T entity) {
        return Response.succeededResponse(getService().list(new QueryWrapper<>(entity)));
    }

    @Operation(summary = "page")
    @PostMapping("page")
    public Response page(Page<T> page, T entity) {
        return Response.succeededResponse(getService().page(page, new QueryWrapper<>(entity)));
    }

    @Operation(summary = "get")
    @PostMapping("get")
    public Response get(T entity) {
        return Response.succeededResponse(getService().getOne(new QueryWrapper<>(entity)));
    }

    @Operation(summary = "get")
    @PostMapping("get/{id}")
    public Response get(@ApiParam(required = true, value = "id")
                        @NotNull(message = "id不能为空")
                        @PathVariable Serializable id) {
        return Response.succeededResponse(getService().getById(id));
    }

    @Operation(summary = "save")
    @PostMapping("save")
    public Response save(@Validated(Insert.class) T entity) {
        getService().save(entity);
        return Response.succeededResponse();
    }

    @Operation(summary = "remove")
    @GetMapping("remove/{id}")
    public Response remove(@ApiParam(required = true, value = "id")
                           @NotNull(message = "id不能为空")
                           @PathVariable Serializable id) {
        getService().removeById(id);
        return Response.succeededResponse();
    }

    @Operation(summary = "modify")
    @PostMapping("modify")
    public Response modify(@Validated(Update.class) T entity) {
        getService().updateById(entity);
        return Response.succeededResponse();
    }

}
